package com.wxc.wxcwzq.web.service;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wxc.wxcwzq.web.entity.SysUser;
import com.wxc.wxcwzq.web.mapper.SysUserMapper;

@Service
public class SysUserService {
	
	@Autowired
	private SysUserMapper mapper;
	
	public SysUser selectByPrimaryKey(Long id){
		mapper.selectByPrimaryKey(id);
		return mapper.selectByPrimaryKey(id);
	}
	
	public SysUser findByUsername(String userName){
		SysUser user = mapper.findByUsername(userName);
		return user;
	}
	
	public Boolean loginAuthentication(String userName,String userPwd){
		Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName,userPwd);
        try{
        	subject.login(token);
            Session session=subject.getSession();
            session.setAttribute("user",token.getPrincipal());
            return true;
        }catch (IncorrectCredentialsException ice) {
            System.out.println("邮箱/密码不匹配！");
            return false;
        } catch (LockedAccountException lae) {
            System.out.println("账户已被冻结！");
            return false;
        } catch (AuthenticationException ae) {
            System.out.println("用户名密码错误！" + ae.getMessage());
        	return false;
        }
	}
	
//	public boolean loginAuthentication(){
//        Subject subject=SecurityUtils.getSubject();
//        if (subject.isAuthenticated()){
//            return true;
//        }else{
//            return false;
//        }
//    }
}
